﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using Microsoft.Phone.Controls;
using Venetasoft.WP.Net;
using System.IO.IsolatedStorage;


namespace WindowsPhoneApplication3
{
    public partial class Email : PhoneApplicationPage
    {
		private List<string> emailtext;
        public Email()
        {
            InitializeComponent();
			emailtext= new List<string>();
        }

        private void Flecha1_Tap(object sender, System.Windows.Input.GestureEventArgs e)
        {
			NavigationService.GoBack();  	
        }

        private void buttonSinc_Click(object sender, System.Windows.RoutedEventArgs e)
        {
            string dia = textBoxDia.Text;
            string mes = textBoxMes.Text;
            string año = textBoxAño.Text;
			try
			{
                if (dia != "" && mes != "" && año != "")
                {
                    string date = dia + "-" + mes + "-" + año + ".txt";
                    // Creo un nuveo objeto MailMessage
                    MailMessage mailMessage = new MailMessage();
                    string EmailID = "lab.sap.uch@gmail.com";
                    string EmailPassword = "labsapuch";

                    mailMessage.Email = EmailID;
                    mailMessage.Password = EmailPassword;

                    mailMessage.AccountType = MailMessage.accountType.Gmail;

                    //Seteo el contenido del mail 
                    mailMessage.To = "lab.sap.uch@gmail.com";
                    mailMessage.Subject = "PHI:" + date;
                    leermail(date);
                    foreach (string n in emailtext) mailMessage.Body += n + "\n\r";
                    //Agrego archivo
                    mailMessage.AddAttachment(@"\DataFolder\" + date);

                    //Envio mail
                    mailMessage.Send();
                    MessageBoxResult Msg;
                    Msg = MessageBox.Show("La información se envio correctamente");
                    emailtext.Clear();
                    textBoxAño.Text = "";
                    textBoxDia.Text = "";
                    textBoxMes.Text = "";
                }
                else { MessageBox.Show("Ingrese datos en todos los campos"); }
			}
			catch
			{
				MessageBox.Show("No es posible sincronizar el dispositivo"  + "\n\r" + "Revise la conexión a internet o los datos ingresados ");
			}
        }
		
        private void leermail(string date)
        {   
            // Código obtenido de http://msdn.microsoft.com/en-us/library/windowsphone/develop/jj714087(v=vs.105).aspx
        	// Obtain a virtual store for the application.
            System.IO.IsolatedStorage.IsolatedStorageFile local = System.IO.IsolatedStorage.IsolatedStorageFile.GetUserStoreForApplication();
            // Specify the file path and options.
            try
            {
                using (var isoFileStream =
                        new System.IO.IsolatedStorage.IsolatedStorageFileStream
                            ("DataFolder\\" + date, System.IO.FileMode.Open, local))
                {
                    // Read the data.
                    using (var isoFileReader = new System.IO.StreamReader(isoFileStream))
                    {
                        string linea;
                        while ((linea = isoFileReader.ReadLine()) != null)
                        {
                            this.emailtext.Add(linea);
                        }
                    }
                }
            }
            catch 
            {
                MessageBox.Show("No se han creado archivos en la fecha seleccionada");
            }
        }
    }
}